﻿#include "hzpch.h"
#include "OpenGLBuffer.h"

#include <glad/glad.h>

namespace Hazal
{
	/// <summary>
	///  OpenGL: Index buffer(IBO)
	/// </summary>
	COpenGLVertexBuffer::COpenGLVertexBuffer(float* pVertices, uint32_t size)
	{
		glCreateBuffers(1, &m_unRendererID);
		glBindBuffer(GL_ARRAY_BUFFER, m_unRendererID);
		glBufferData(GL_ARRAY_BUFFER, size, pVertices, GL_STATIC_DRAW);
	}

	COpenGLVertexBuffer::COpenGLVertexBuffer(uint32_t unSize)
	{
		glCreateBuffers(1, &m_unRendererID);
		glBindBuffer(GL_ARRAY_BUFFER, m_unRendererID);
		glBufferData(GL_ARRAY_BUFFER, unSize, nullptr, GL_DYNAMIC_DRAW);
	}

	COpenGLVertexBuffer::~COpenGLVertexBuffer()
	{
		glDeleteBuffers(1, &m_unRendererID);
	}

	void COpenGLVertexBuffer::Bind() const
	{
		glBindBuffer(GL_ARRAY_BUFFER, m_unRendererID);
	}

	void COpenGLVertexBuffer::Unbind() const
	{
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	void COpenGLVertexBuffer::SetLayout(const CBufferLayout& rLayout)
	{
		m_layout = rLayout;
	}

	const CBufferLayout& COpenGLVertexBuffer::GetLayout() const
	{
		return m_layout;
	}

	void COpenGLVertexBuffer::SetData(const void* pData, uint32_t unDataSize)
	{
		Bind();
		glBufferSubData(GL_ARRAY_BUFFER, 0, unDataSize, pData);
	}

	/// <summary>
	///  OpenGL: Index buffer(IBO)
	/// </summary>
	COpenGLIndexBuffer::COpenGLIndexBuffer(uint32_t* pIndices, uint32_t count)
		: m_unCount(count)
	{
		glCreateBuffers(1, &m_unRendererID);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_unRendererID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, count * sizeof(uint32_t), pIndices, GL_STATIC_DRAW);
	}

	COpenGLIndexBuffer::~COpenGLIndexBuffer()
	{
		glDeleteBuffers(1, &m_unRendererID);
	}

	void COpenGLIndexBuffer::Bind() const
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_unRendererID);
	}

	void COpenGLIndexBuffer::Unbind() const
	{
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	}
	uint32_t COpenGLIndexBuffer::GetCount() const
	{
		return m_unCount;
	}
}